package com.archstudy.springcloud.websocket.controller;

import com.archstudy.push.platform.contract.dto.MsgDTO;
import com.archstudy.springcloud.common.constants.SessionConstants;
import com.archstudy.springcloud.common.dto.UserDTO;
import com.archstudy.springcloud.common.exception.BusinessException;
import com.archstudy.springcloud.common.result.RestOut;
import com.archstudy.springcloud.common.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(value = "基础学习DEMO", tags = {"DEMO"})
@RestController
@RequestMapping("/api/other")
public class OtherController
{
    /**
     * 注入全局的加密器
     */
    @Resource
    PasswordEncoder passwordEncoder;


    @ApiOperation(value = "接口的功能介绍", notes = "提示接口使用者注意事项", httpMethod = "GET")
    @ApiImplicitParam(dataType = "string", name = "name", value = "姓名", required = true)
    @RequestMapping(value = "/hello/v1")
    public String hello(String name)
    {
        log.info("方法 hello 被调用了");

        return "hello " + name;
    }

    @ApiOperation(value = "接口的功能介绍", notes = "提示接口使用者注意事项", httpMethod = "GET")
    @ApiImplicitParam(dataType = "string", name = "name", value = "姓名", required = true)
    @RequestMapping(value = "/health/v1")
    public String health(String name)
    {
        log.info("方法 hello 被调用了");

        return "hello " + name;
    }

    @PostMapping("/push/rpc")
    @ApiOperation(value = "rpc发送简单消息")
    public RestOut<String> pushByRpc(@RequestBody MsgDTO dto, HttpServletRequest request) {

        String appId = request.getHeader(SessionConstants.USER_IDENTIFIER);
        if (null == appId) {
            throw BusinessException.builder().errMsg("参数不对，需要 appId").build();
        }
        dto.setAppId(Long.valueOf(appId));
//        mqMessageService.saveAndDispatchMessage(dto);

        log.info("仅仅用于rpc演示，appId: {}, dto {}", appId, JsonUtil.pojoToJson(dto));

        return RestOut.success("发送完成");
    }

}
